﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ServeurRSS_1_0.Source.Model;

namespace ServeurRSS_1_0
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service_DirectoryManagement" in code, svc and config file together.
    public class Service_DirectoryManagement : IServiceDirectoryManagement
    {
        private DirectoryModel model
        {
            get;
            set;
        }

        public Service_DirectoryManagement()
        {
            model = new DirectoryModel();
        }

        public string createDirectory(System.Guid userId, DataDirectory newDir, DataDirectory parent = null)
        {
            try
            {
               return model.createDirectory(userId, newDir, parent);
            }
            catch (Exception e)
            {
                return "NOK " +e.Message;
            }
        }


        public  string eraseDirectory(System.Guid dir, System.Guid user)
        {
            try
            {
                LinkServeurRSSDataContext db = new LinkServeurRSSDataContext();

                DirectoriesUser userDir =  db.DirectoriesUsers.Single(elem => elem.id == dir && elem.fk_id_Directories_User == user);

                var list = db.DirectoriesRsses.Where(elem => elem.fk_id_Directories == dir);
                                                   
                db.DirectoriesRsses.DeleteAllOnSubmit<DirectoriesRss>(list);    
                db.DirectoriesUsers.DeleteOnSubmit(userDir);

                
                db.SubmitChanges();
                return "OK";
            }
            catch (Exception e)
            {
               throw new Exception("NOK " + e.Message, e);
            }
        }


        public RssTree getDirectoriesUser(System.Guid idUser)
        {

            try
            {
                RssTree dir = model.getAll(idUser);
                return dir;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public RssTree moveDirectoy(System.Guid newParent, System.Guid dir)
        {
            try
            {
                LinkServeurRSSDataContext db = new LinkServeurRSSDataContext();
                DirectoriesUser current = db.DirectoriesUsers.Single(d => d.id == dir);
                if (newParent == System.Guid.Empty) 
                    current.fk_id_Directories_Parent = null;
                else
                    current.fk_id_Directories_Parent = newParent;

                System.Guid idUser = (System.Guid)current.fk_id_Directories_User;
                db.SubmitChanges();

                return this.getDirectoriesUser(idUser);
            }
            catch (Exception e)
            {
                return null;
            }
        }

    }
}
